#!/usr/bin/python

from __future__ import with_statement
from cStringIO import StringIO
#from contrib.sphinxapi import *
import sqlalchemy as sa
from Queue import Queue
from threading import Thread
import threading
from datetime import datetime, date,timedelta
from time import strftime
import re, traceback, calendar, time, sys

#from local
from lib.database_msn import Database, g_pagesize

from lib.utils import sanitizestring, transcode3, transcode5
#from lib.qqaddr import getProvinceIdx, getCityIdx, getCountryIdx

# type obj
class scheme:
    docid = 'seq'
    type = 'type'

csft_string_const = 'csft_string_const'
csft_string_fulltext = 'csft_string_fulltext'
csft_number = 'csft_number'
csft_float = 'csft_float'
csft_boolean = 'csft_boolean'
csft_timestamp = 'csft_timestamp'
csft_list = 'csft_list'
csft_file_pdf = 'csft_file_pdf'
csft_file_office = 'csft_file_office'

class MsnBlog:
    def __init__(self, O):
        self.include_info = True
        self.i = 0

        self.seq = 0
        self.plaintext = ''
        self.title = ''
        self.authorseq = -1
        self.sex = 0
        self.age = 0

        self.dal = Database()

        self.cursor = 0
        self.workingset = []

        #for k in O.keys():
        #    print k, O[k] 

    def getScheme(self):
        if self.include_info:
            return {'seq': {'seq': True},
                'authorseq': {'type': csft_number},
                'sex': {'type': csft_number},
                'age': {'type': csft_number},
                'title': {'type': csft_string_fulltext},
                'plaintext': {'type': csft_string_fulltext}}
        else:
            return {'seq': {'seq': True},
                'authorseq': {'type': csft_number},
                'title': {'type': csft_string_fulltext},
                'plaintext': {'type': csft_string_fulltext}}

    def test(self):
         blogs = self.dal._queryBlog(0)
         # if there are no bookmarks by user, let's forget it.
         #print len(blogs)
         print blogs

    def Connected(self):
        pass

    def NextDocument(self):
        while True:
            barrier = len(self.workingset) if (len(self.workingset) < g_pagesize) else g_pagesize
            #print len(self.workingset),g_pagesize,barrier,self.cursor
            if self.cursor >= barrier:
                self.workingset = self.dal._queryBlog(self.seq)
                #print self.workingset
                if not self.workingset:
                    return False
                else:
                    self.i += 1
                    self.cursor = 0

            b = self.workingset[self.cursor]
            [seq, id, blogid, title, url, time, num_comment, blog_type, content, plaintext] = b
            self.cursor += 1

#            print self.i
            oneuser = self.dal._queryOneUser(id)
            if oneuser:
                self.authorseq = oneuser[0]
                if self.include_info:
                    if oneuser[5] == 1 or oneuser[5] == 2:
                        self.sex = oneuser[5]
                    else:
                        self.sex = 0
                    if oneuser[6] > 0 and oneuser[6] < 100:
                        self.age = oneuser[6]
                    else:
                        self.age = 0
            else:
                print blogid + " " + str(seq) + " didn't find authorid " + id
                continue

            self.seq = seq
            self.title = title.encode('utf-8') if isinstance(title, unicode) else title
            self.plaintext = plaintext.encode('utf-8') if isinstance(plaintext, unicode) else plaintext
            #print 'seq',seq,'title',title,'plain',plaintext
            if self.title == '':
                continue

            return True

    def OnBeforeIndex(self):
        pass

    def OnAfterIndex(self):
        pass

    def OnIndexFinished(self):
        del self.dal
        self.dal = None

def Test():
    MsnB=MsnBlog(None)
    MsnB.test()
    #MsnB.test(100104)
#Test()
